<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.6"/>
<title>LiquidFun Programmer&#39;s Guide: Main Page</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
  $(window).load(resizeHeight);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td style="padding-left: 0.5em;">
   <div id="projectname">LiquidFun Programmer&#39;s Guide
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.6 -->
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('index.html','');});
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">LiquidFun Programmer's Guide</div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><div class="image">
<img src="liquidfun-logo-square-small.png"  alt="LiquidFun logo" style="float:right;"/>
</div>
<p><a href="#about">About</a><br/>
 <a href="#pre">Prerequisites</a><br/>
 <a href="#atm">About this manual</a><br/>
 <a href="#frb">Feedback and reporting bugs</a><br/>
 <a href="#cc">Core concepts</a><br/>
 <a href="#mo">Modules</a><br/>
 <a href="#un">Units</a><br/>
 <a href="#fd">Factories and definitions</a><br/>
</p>
<p><a class="anchor" id="About"></a><br/>
</p>
<h2>About</h2>
<p>LiquidFun is based on Erin Catto's <a href="http://www.box2d.org">Box2D library</a>, which provides 2D, rigid-body simulation in games. LiquidFun extends Box2D to provide <a href="md__chapter11__particles.html">particle physics and fluid dynamics</a>.</p>
<p>Programmers can use LiquidFun in their games to make objects move in realistic ways and make the game world more interactive. From the game engine's point of view, a physics engine is just a system for procedural animation.</p>
<p>LiquidFun is written in portable C++. Most of the types defined in the engine begin with the b2 prefix. Hopefully this is sufficient to avoid name clashing with your game engine.</p>
<p><a class="anchor" id="pre"></a><br/>
 </p>
<h2>Prerequisites</h2>
<p>In this manual I'll assume you are familiar with basic physics concepts, such as mass, force, torque, and impulses. If not, please first consult Google search and Wikipedia.</p>
<p>LiquidFun is based on the Box2D library, which was created as part of a physics tutorial at the Game Developer Conference. You can get these tutorials from the download section of Box2D.org.</p>
<p>Since LiquidFun is written in C++, you are expected to be experienced in C++ programming. LiquidFun should not be your first C++ programming project! You should be comfortable with compiling, linking, and debugging.</p>
<p>Caution</p>
<p>LiquidFun should not be your first C++ project. Please learn C++ programming, compiling, linking, and debugging before working with LiquidFun. There are many resources for this on the net.</p>
<p><a class="anchor" id="atm"></a><br/>
</p>
<h2>About this manual</h2>
<p>This manual covers the majority of the LiquidFun API. However, not every aspect is covered. You are encouraged to look at the testbed included with LiquidFun to learn more. Also, the LiquidFun code base has comments formatted for Doxygen, so it is easy to create a hyper-linked API document.</p>
<p>This manual is only updated with new releases. The version in source control is likely to be out of date.</p>
<p><a class="anchor" id="frb"></a><br/>
 </p>
<h2>Feedback and Reporting Bugs</h2>
<p>If you have a question or feedback about LiquidFun, please leave a comment in the forum. This is also a great place for community discussion.</p>
<p>LiquidFun issues are tracked using a Google code project. This is a great way to track issues and ensures that your issue will not be lost in the depths of the forums.</p>
<p>Please file bugs and feature requests here: <a href="http://github.com/google/liquidfun/issues">http://github.com/google/liquidfun/issues</a></p>
<p>You can help to ensure your issue gets fixed if you provide sufficient detail. A testbed example that reproduces the problem is ideal. You can read about the testbed later in this document.</p>
<p><a class="anchor" id="cc"></a><br/>
 </p>
<h2>Core Concepts</h2>
<p>LiquidFun works with several fundamental concepts and objects. We briefly define these objects here and more details are given later in this document.<br/>
 <br/>
 </p>
<h3>shape</h3>
<p>A shape is 2D geometrical object, such as a circle or polygon.<br/>
</p>
<h3>rigid body</h3>
<p>A chunk of matter that is so strong that the distance between any two bits of matter on the chunk is constant. They are hard like a diamond. In the following discussion we use body interchangeably with rigid body.<br/>
</p>
<h3>fixture</h3>
<p>A fixture binds a shape to a body and adds material properties such as density, friction, and restitution. A fixture puts a shape into the collision system (broad-phase) so that it can collide with other shapes.<br/>
</p>
<h3>constraint</h3>
<p>A constraint is a physical connection that removes degrees of freedom from bodies. A 2D body has 3 degrees of freedom (two translation coordinates and one rotation coordinate). If we take a body and pin it to the wall (like a pendulum) we have constrained the body to the wall. At this point the body can only rotate about the pin, so the constraint has removed 2 degrees of freedom.<br/>
</p>
<h3>contact constraint</h3>
<p>A special constraint designed to prevent penetration of rigid bodies and to simulate friction and restitution. You do not create contact constraints; they are created automatically by LiquidFun.<br/>
</p>
<h3>joint</h3>
<p>This is a constraint used to hold two or more bodies together. LiquidFun supports several joint types: revolute, prismatic, distance, and more. Some joints may have limits and motors.<br/>
</p>
<h3>joint limit</h3>
<p>A joint limit restricts the range of motion of a joint. For example, the human elbow only allows a certain range of angles.<br/>
</p>
<h3>joint motor</h3>
<p>A joint motor drives the motion of the connected bodies according to the joint's degrees of freedom. For example, you can use a motor to drive the rotation of an elbow.<br/>
</p>
<h3>world</h3>
<p>A physics world is a collection of bodies, fixtures, and constraints that interact together. LiquidFun supports the creation of multiple worlds, but this is usually not necessary or desirable.<br/>
</p>
<h3>solver</h3>
<p>The physics world has a solver that is used to advance time and to resolve contact and joint constraints. The LiquidFun solver is a high performance iterative solver that operates in order N time, where N is the number of constraints.<br/>
</p>
<h3>continuous collision</h3>
<p>The solver advances bodies in time using discrete time steps. Without intervention this can lead to tunneling.<br/>
 <br/>
</p>
<div class="image">
<img src="image_0.png"  align="center" alt="Tunneling" height="293" width="275"/>
</div>
<p><br/>
</p>
<p>LiquidFun contains specialized algorithms to deal with tunneling. First, the collision algorithms can interpolate the motion of two bodies to find the first time of impact (TOI). Second, there is a sub-stepping solver that moves bodies to their first time of impact and then resolves the collision.</p>
<p><a class="anchor" id="mo"></a><br/>
 </p>
<h2>Modules</h2>
<p>LiquidFun is composed of three modules: Common, Collision, and Dynamics. The Common module has code for allocation, math, and settings. The Collision module defines shapes, a broad-phase, and collision functions/queries. Finally the Dynamics module provides the simulation world, bodies, fixtures, and joints.<br/>
 <br/>
</p>
<div class="image">
<img src="image_1.png"  align="center" alt="Modules" height="229" width="217"/>
</div>
<p> <a class="anchor" id="un"></a><br/>
 </p>
<h2>Units</h2>
<p>LiquidFun works with floating point numbers and tolerances have to be used to make LiquidFun perform well. These tolerances have been tuned to work well with meters-kilogram-second (MKS) units. In particular, LiquidFun has been tuned to work well with moving shapes between 0.1 and 10 meters. So this means objects between soup cans and buses in size should work well. Static shapes may be up to 50 meters long without trouble.</p>
<p>Being a 2D physics engine, it is tempting to use pixels as your units. Unfortunately this will lead to a poor simulation and possibly weird behavior. An object of length 200 pixels would be seen by LiquidFun as the size of a 45 story building.</p>
<p>Caution</p>
<p>LiquidFun is tuned for MKS units. Keep the size of moving objects roughly between 0.1 and 10 meters. You'll need to use some scaling system when you render your environment and actors. The LiquidFun testbed does this by using an OpenGL viewport transform. DO NOT USE PIXELS.</p>
<p>It is best to think of LiquidFun bodies as moving billboards upon which you attach your artwork. The billboard may move in a unit system of meters, but you can convert that to pixel coordinates with a simple scaling factor. You can then use those pixel coordinates to place your sprites, etc. You can also account for flipped coordinate axes.</p>
<p>LiquidFun uses radians for angles. The body rotation is stored in radians and may grow unbounded. Consider normalizing the angle of your bodies if the magnitude of the angle becomes too large (use b2Body::SetAngle).</p>
<p>Caution</p>
<p>LiquidFun uses radians, not degrees.</p>
<p><a class="anchor" id="fd"></a><br/>
 </p>
<h2>Factories and Definitions</h2>
<p>Fast memory management plays a central role in the design of the LiquidFun API. So when you create a b2Body or a b2Joint, you need to call the factory functions on b2World. You should never try to allocate these types in another manner.</p>
<p>There are creation functions:</p>
<p>&#160;&#160;&#160;<code>b2Body* b2World::CreateBody(const b2BodyDef* def)</code><br/>
 &#160;&#160;&#160;<code>b2Joint* b2World::CreateJoint(const b2JointDef* def)</code><br/>
 &#160;&#160;&#160;<code>And there are corresponding destruction functions:</code><br/>
 &#160;&#160;&#160;<code>void b2World::DestroyBody(b2Body* body)</code><br/>
 &#160;&#160;&#160;<code>void b2World::DestroyJoint(b2Joint* joint)</code><br/>
</p>
<p>When you create a body or joint, you need to provide a definition. These definitions contain all the information needed to build the body or joint. By using this approach we can prevent construction errors, keep the number of function parameters small, provide sensible defaults, and reduce the number of accessors.</p>
<p>Since fixtures (shapes) must be parented to a body, they are created and destroyed using a factory method on b2Body:</p>
<p>&#160;&#160;&#160;<code>b2Fixture* b2Body::CreateFixture(const b2FixtureDef* def)</code><br/>
 &#160;&#160;&#160;<code>void b2Body::DestroyFixture(b2Fixture* fixture)</code><br/>
</p>
<p>There is also shortcut to create a fixture directly from the shape and density.</p>
<p>&#160;&#160;&#160;<code>b2Fixture* b2Body::CreateFixture(const b2Shape* shape, float32 density)</code><br/>
</p>
<p>Factories do not retain references to the definitions. So you can create definitions on the stack and keep them in temporary resources.</p>
<p><em>This content is licensed under <a href="http://creativecommons.org/licenses/by/4.0/legalcode">Creative Commons Attribution 4.0</a>. For details and restrictions, please see the <a href="md__content_license.html">Content License</a>.</em></p>
<p><a href="md__chapter02__hello__box2_d.html"></a> <a href="md__chapter03__common.html"></a> <a href="md__chapter04__collision__module.html"></a> <a href="md__chapter05__dynamics__module.html"></a> <a href="md__chapter06__bodies.html"></a> <a href="md__chapter07__fixtures.html"></a> <a href="md__chapter08__joints.html"></a> <a href="md__chapter09__contacts.html"></a> <a href="md__chapter10__world.html"></a> <a href="md__chapter11__particles.html"></a> <a href="md__chapter12__loose__ends.html"></a> <a href="md__chapter13__debug__drawing.html"></a> <a href="md__chapter14__limitations.html"></a> <a href="md__chapter15__references.html"></a> </p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- Google Analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-46159502-1', 'auto');
ga('create', 'UA-49880327-7', 'auto', {'name': 'liquidFunTracker'});
ga('send', 'pageview');
ga('liquidFunTracker.send', 'pageview');
</script>
</body>
</html>
